package com.reagent.system.service.impl;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reagent.response.Result;
import com.reagent.system.entity.Goods;
import com.reagent.system.entity.vo.CommonResult;
import com.reagent.system.entity.vo.Goods_s;
import com.reagent.system.entity.vo.QyeryInfo;
import com.reagent.system.mapper.GoodsMapper;
import com.reagent.system.mapper.TypeMapper;
import com.reagent.system.mapper.UserMapper;
import com.reagent.system.service.GoodsService;
import com.reagent.utils.FileUtils;
import com.reagent.utils.JWTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Transactional
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private TypeMapper typeMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Object addGoods(MultipartFile images, Goods goods) throws Exception {
        String token = JWTUtils.getCurrentToken();
        if (!ObjectUtils.isEmpty(token)) {
            DecodedJWT jwt = JWTUtils.verifyToken(token);
            if (!ObjectUtils.isEmpty(jwt)) {
                // 图片上传
                String uploadPath = FileUtils.FileUploadOne(images);
                Date date = new Date();
                goods.setImages(uploadPath)
                        .setStatus(0)
                        .setUpdatetime(date)
                        .setCreatetime(date)
                        .setMid(Integer.parseInt(jwt.getClaim("id").asString()));
                log.info("上传信息：" + goods);
                int insert = goodsMapper.insert(goods);
                if (insert > 0) {
                    log.info("添加商品成功!");
                    return Result.success().message("添加商品成功!");
                }
                log.error("添加商品失败!");
                return Result.error().message("添加商品失败!");
            }
            log.error("token过期");
            return Result.error().message("token过期");
        }
        log.error("token不存在");
        return Result.error().message("token不存在");
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object getGoodsInfoUid(Integer pageSize, Integer currentPage, String serachInfo) {
        String token = JWTUtils.getCurrentToken();
        if (!ObjectUtils.isEmpty(token)) {
            DecodedJWT jwt = JWTUtils.verifyToken(token);
            if (!ObjectUtils.isEmpty(jwt)) {
                List<Goods> list = goodsMapper.getGoodsInfoUid(new QyeryInfo()
                        .setPageSize(pageSize)
                        .setCurrentPage((currentPage - 1) * pageSize)
                        .setSerachInfo(serachInfo), Integer.parseInt(jwt.getClaim("id").asString()));
                ArrayList<Goods_s> objects = new ArrayList<>();
                if (!ObjectUtils.isEmpty(list)) {
                    for (Goods goods : list) {
                        Goods_s s = new Goods_s();
                        BeanUtils.copyProperties(goods, s);
                        s.setType(typeMapper.selectById(goods.getType()).getName())
                                .setMid(userMapper.selectById(goods.getMid()).getUsername());
                        objects.add(s);
                    }
                    log.info("查询成功!");
                    CommonResult result = new CommonResult();
                    result.setList(objects).setPageTotal(goodsMapper.selectCount(null));
                    return Result.success().message("查询成功!").data(result);
                }
                log.warn("暂无数据!");
                return Result.success().message("暂无数据!");
            }
        }
        return Result.error();
    }


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveInfo(List<Goods> list) {
        Integer integer = goodsMapper.batchInsert(list);
        if (integer > 0) {
            log.info("添加成功");
        } else {
            log.info("添加失败");
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Object getGoodsInfo(Integer pageSize, Integer currentPage, String serachInfo) {
        List<Goods> list = goodsMapper.getGoodsInfoAll(new QyeryInfo()
                .setSerachInfo(serachInfo)
                .setCurrentPage((currentPage - 1) * pageSize)
                .setPageSize(pageSize));
        if (!ObjectUtils.isEmpty(list)) {
            log.info("查询成功!");
            CommonResult result = new CommonResult();
            result.setList(list).setPageTotal(goodsMapper.selectCount(new QueryWrapper<Goods>().select("*").eq(
                    "status", 1)));
            return Result.success().message("查询成功!").data(result);
        } else {
            log.warn("暂无数据!");
            return Result.error().message("暂无数据!");
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Object updateStatus1(Integer id) {
        Integer integer = goodsMapper.updateStatus1(id);
        if (integer > 0) {
            return Result.success().message("上架成功!");
        }
        return Result.error().message("上架失败!");
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Object updateStatus0(Integer id) {
        Integer integer = goodsMapper.updateStatus0(id);
        if (integer > 0) {
            return Result.success().message("下架成功!");
        }
        return Result.error().message("下架失败!");
    }
}
